home *** CD-ROM | disk | FTP | other *** search
/ Maclife 157 / MACLIFE157-2001-09.ISO.7z / MACLIFE157-2001-09.ISO / Linux / MacOS Tools / BootX 1.2.2 / Sources / src / miboot / miBoot_boot3.c < prev    next >
C/C++ Source or Header  |  2001-07-23  |  5KB  |  218 lines

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <Quickdraw.h>
  4. #include <Palettes.h>
  5. #include <Fonts.h>
  6. #include <Events.h>
  7. #include <Menus.h>
  8. #include <Errors.h>
  9. #include <Files.h>
  10. #include <Devices.h>
  11. #include <Windows.h>
  12. #include <Icons.h>
  13. #include <LowMem.h>
  14. #include <a4Stuff.h>
  15.  
  16. #include "debug_text.h"
  17. #include "nr_wrapper.h"
  18.  
  19. #define VERSION        "1.0a3"
  20.  
  21. static void set_a5_world(void);
  22. static void test_paint(void);
  23. static void setup_debug_context(Boolean debug_visible);
  24. static void show_context(void);
  25. static void    setup_quickdraw(void);
  26. static void    close_quickdraw(void);
  27. static void draw_penguin(short whichPenguin);
  28. static void    load_boot4(void);
  29. static void    check_opt_keys(void);
  30.  
  31. /* Globals */
  32. dt_context*    dct;
  33. QDGlobals    qd;
  34. GrafPtr        port;
  35.  
  36. dt_context    g_dct;
  37. CGrafPort    g_grafport;
  38. Boolean        g_use_color = 1;
  39. Boolean        g_debug_visible = 0;
  40. Boolean        g_no_video = 0;
  41.  
  42. #define kPenguinNormalID        128
  43. #define kPenguinRamDiskID        129
  44. #define kPenguinErrorID            130
  45.  
  46. void main(void)
  47. {
  48.     EnterCodeResource();
  49.     
  50.     check_opt_keys();
  51.     setup_debug_context(g_debug_visible);
  52.     
  53.     dt_printf(dct, "Welcome to miBoot v" VERSION "¥n¥n");
  54.     
  55.     show_context();
  56.     
  57.     dt_printf(dct, "Setting up drawing environment...");
  58.     setup_quickdraw();
  59.     dt_printf(dct, "ok.¥n");
  60.     dt_printf(dct, "Drawing penguin...");
  61.     draw_penguin(kPenguinNormalID);
  62.     dt_printf(dct, "ok.¥n");
  63.  
  64.     load_boot4();
  65.     
  66.     draw_penguin(kPenguinErrorID);
  67.     dt_printf(dct, "¥nQuitting...¥n");    
  68.     close_quickdraw();
  69.     
  70.     ExitCodeResource();
  71. }
  72.  
  73. void
  74. load_boot4(void)
  75. {
  76.     Handle    rsrc = GetResource('boot', 4);
  77.     if (!rsrc) {
  78.         dt_printf(dct, "GetResource('boot', 4) error %d¥n", ResError());
  79.         return;
  80.     }
  81.     DetachResource(rsrc);
  82.     MoveHHi(rsrc);
  83.     HLock(rsrc);
  84.     
  85.     ((void (*)(dt_context*, Boolean))(*rsrc))(dct, g_no_video);
  86.     
  87.     DisposeHandle(rsrc);
  88. }
  89.  
  90. void
  91. draw_penguin(short whichPenguin)
  92. {
  93.     CIconHandle    icon;
  94.     Rect        bounds;
  95.     
  96.     icon = GetCIcon(whichPenguin);
  97.     if (!icon) {
  98.         dt_printf(dct, "Can't load penguin icon (ResErr: %d) !¥n",
  99.             ResError());
  100.         return;
  101.     }
  102.  
  103.     bounds.left = port->portRect.left + (port->portRect.right - port->portRect.left)/2 - 16;
  104.     bounds.top = port->portRect.top + (port->portRect.bottom - port->portRect.top)/2 - 16;
  105.     bounds.right = bounds.left + 32;
  106.     bounds.bottom = bounds.top + 32;
  107.     
  108.     FillRect(&bounds, &qd.gray);
  109.  
  110.     PlotCIcon(&bounds, icon);
  111.     DisposeCIcon(icon);
  112. }
  113.  
  114. asm void
  115. set_a5_world(void)
  116. {
  117. #define kLM_CurrentA5    0x904
  118.     lea        qd, a0
  119.     adda.l    #struct(QDGlobals.thePort), a0
  120.     move.l    a5, kLM_CurrentA5
  121.     rts
  122. }
  123.  
  124. void
  125. setup_quickdraw(void)
  126. {
  127.     set_a5_world();
  128.     InitGraf(&qd.thePort);
  129.     InitPalettes();
  130.     port = (GrafPtr)&g_grafport;
  131.     if (g_use_color)
  132.         OpenCPort(&g_grafport);
  133.     else
  134.         OpenPort(port);
  135.     SetPort(port);
  136.     PenNormal();
  137. }
  138.  
  139. void
  140. close_quickdraw(void)
  141. {
  142.     if (g_use_color)
  143.         CloseCPort(&g_grafport);
  144.     else
  145.         ClosePort(port);
  146. }
  147.  
  148. void
  149. setup_debug_context(Boolean debug_visible)
  150. {
  151.     GDHandle    hdl;
  152.     PixMapPtr    pm;
  153.     
  154.     hdl = LMGetMainDevice();
  155.     if (hdl == NULL || (**hdl).gdPMap == NULL)
  156.         return;
  157.     pm = *(**hdl).gdPMap;
  158.     if (pm->baseAddr == NULL)
  159.         return;
  160.     
  161.     dct = &g_dct;
  162.     
  163.     dct->base = (unsigned char *)pm->baseAddr;
  164.     dct->row_bytes = pm->rowBytes & 0x3fff;
  165.     dct->width = pm->bounds.right - pm->bounds.left;
  166.     dct->height = pm->bounds.bottom - pm->bounds.top;
  167.     dct->depth = pm->pixelSize;
  168.     dct->visible = debug_visible;
  169.     
  170.     if (dct->depth == 15)
  171.         dct->depth = 16;
  172.     
  173.     dct->base += pm->bounds.top * dct->row_bytes;
  174.     dct->base += pm->bounds.left * (dct->depth >> 3);
  175.     
  176.     dt_init(dct);
  177. }
  178.  
  179. asm UInt32 read_sp(void)
  180. {
  181.     move.l    sp, d0
  182.     rts
  183. }
  184.  
  185. void
  186. show_context(void)
  187. {
  188.     dt_printf(dct, "SystemZone start : 0x%08lx¥n", LMGetSysZone()->bkLim);
  189.     dt_printf(dct, "SystemZone free  : 0x%08lx¥n", LMGetSysZone()->zcbFree);
  190.     dt_printf(dct, "CurrenZone start : 0x%08lx¥n", LMGetTheZone()->bkLim);
  191.     dt_printf(dct, "CurrenZone free  : 0x%08lx¥n", LMGetTheZone()->zcbFree);
  192.     dt_printf(dct, "ApplZone start   : 0x%08lx¥n", LMGetApplZone()->bkLim);
  193.     dt_printf(dct, "ApplZone free    : 0x%08lx¥n", LMGetApplZone()->zcbFree);
  194.     dt_printf(dct, "ApplLimit        : 0x%08lx¥n", LMGetApplLimit());
  195.     dt_printf(dct, "CurStackBase     : 0x%08lx¥n", LMGetCurStackBase());
  196.     dt_printf(dct, "BufPtr           : 0x%08lx¥n", LMGetBufPtr());
  197.     dt_printf(dct, "MemTop           : 0x%08lx¥n", LMGetMemTop());
  198.     dt_printf(dct, "-> limit         : 0x%08lx¥n", ((UInt32)LMGetMemTop()/2 + 1024));
  199.     dt_printf(dct, "sp               : 0x%08lx¥n", read_sp());
  200.     dt_printf(dct, "-> free          : 0x%08lx¥n", read_sp() - (UInt32)LMGetApplLimit());
  201. }
  202.  
  203. static check_one_key(KeyMap map, UInt8 key_code)
  204. {
  205.     return ((((UInt8 *)map)[key_code >> 3] >> (key_code & 7)) & 1);
  206. }
  207.  
  208. void
  209. check_opt_keys(void)
  210. {
  211.     KeyMap    keys;
  212.     
  213.     GetKeys(keys);
  214.     if (check_one_key(keys, 0x3a)) // option
  215.         g_debug_visible = 1;
  216.     if (check_one_key(keys, 0x38)) // shift
  217.         g_no_video = 1;
  218. }